package com.ogilvy.audi.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ogilvy.audi.bean.Circle;
import com.ogilvy.audi.bean.Topic;
import com.ogilvy.audi.service.CircleService;
import com.ogilvy.audi.service.TopicService;
import com.ogilvy.audi.util.MethodTools;
import com.ogilvy.frame.action.BaseAction;
import com.ogilvy.frame.page.PageTools;
import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("serial")
@Scope("prototype")
@Controller("auti.action.CircleAction")
public class CircleAction extends BaseAction {
	public String id;
	public String parentId;
	public Circle circle;
	public List<Circle> cirList;
	public List<Circle> hostList;
	public List<?> topList;
	public List<Topic> topListSimple;
	public String msg;
	@Autowired
	protected CircleService circleService;
	@Autowired
	protected TopicService topicService;

	public String queryAllCircle() {
		System.out.println(id);
		PageTools<Circle> plist = circleService.query();
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		request.setAttribute("plist", plist);
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("circleId", 1);
		PageTools<Topic> tlist = topicService.query(1, 5, condition);
		request.setAttribute("tlist", tlist);
		return "result";
	}

	public String query() {
		System.out.println("______________________________++++++++++++++++______________"+circle);
		/*PageTools<Circle> plist = circleService.query();
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		request.setAttribute("plist", plist);
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("circleId", 1);
		PageTools<Topic> tlist = topicService.query(1, 5, condition);
		request.setAttribute("tlist", tlist);*/
		return "result";
	}

	// 查询一级圈子，在首页显示
	public String queryMainCircles() {
		System.out.println(id);
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("parentId", "-1");
		PageTools<Circle> plist = circleService.query(1, 5, condition);
		cirList = plist.getList();
		condition.put("isHot", 1);
		plist = circleService.query(1, 5, condition);
		hostList = plist.getList();
		condition = new HashMap<String, Object>();
		PageTools<Topic> plistt = topicService.query(1, 5, condition);
		/* 关联查询时hibernate会返回List<Object>,以下是遍历集合取第一个主对象的方法* */
		topList = plistt.getList();
		Object[] joinObj;
		Iterator itr = topList.iterator();
		topListSimple = new ArrayList();
		while (itr.hasNext()) {
			joinObj = (Object[]) itr.next();
			topListSimple.add((Topic) joinObj[0]);
		}
		// 查询相册
		return "cindex";
	}

	// 查询热门圈子，在首页显示
	public String queryHotCircles() {
		System.out.println(id);
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("isHot", "1");
		PageTools<Circle> plist = circleService.query(1, 3, condition);
		// HttpServletRequest request =
		// (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		// request.setAttribute("hotlist", plist);
		return "result";
	}

	// 查询二级圈子列表页
	public String queryChildCircles() {
		System.out.println("_______________________________________________查询二级圈子");
		Map<String, Object> condition = new HashMap<String, Object>();
		parentId = MethodTools.nullStr(parentId);
		if (parentId.equals("")) {
			System.out.println("parentId 为空...");
			msg = "parentId 不能为空";
			/*HttpServletRequest request =
					 (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
					 request.setAttribute("msg", msg);*/
			return "audiResult";
		}
		condition.put("parentId", id);
		cirList = circleService.queryChidCircles(Integer.parseInt(parentId));
		circle = circleService.get(Integer.parseInt(parentId));

		return "childList";
	}
	// 查询二级圈子的详细内容(返回子圈子首页)
		public String queryDetailCircles() {
			System.out.println("查询子圈子详细内容");
			Map<String, Object> condition = new HashMap<String, Object>();
			MethodTools.nullStr(parentId);
			if (parentId.equals("")) {
				msg = "parentId 不能为空";
				return "audiResult";
			}
			condition.put("parentId", parentId);
			cirList = circleService.queryChidCircles(Integer.parseInt(parentId));
			System.out.println("________________________"+circle.getCirclDesc());
			return "childList";
		}

	
	
	public String add() {
		Circle circle = new Circle();
		circleService.add(circle);
		return null;
	}

	public String del() {
		circleService.del(Integer.parseInt(id));
		return null;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}
